package cn.itcast.p2p20.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.bean.AllProductDataBean;
import cn.itcast.p2p20.ui.ProgressRing;

/**
 * Created by Administrator on 2016/7/26.
 */
public class AllProductListAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<AllProductDataBean> allProductDataBeenList;

    public AllProductListAdapter(Context mContext, List<AllProductDataBean> allProductDataBeenList) {
        this.mContext = mContext;
        this.allProductDataBeenList = allProductDataBeenList;
    }


    //三种类型的ViewHolder 返回的viewholer 由viewType来决定
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {

            case AllProductDataBean.productTypeFinance:
                viewHolder = new FinanceViewHolder(View.inflate(mContext, R.layout.item_finance, null));
                break;
            case AllProductDataBean.productTypeInvest:
                viewHolder = new InvestViewHolder(View.inflate(mContext, R.layout.item_invest, null));
                break;
            case AllProductDataBean.productTypeFund:
                viewHolder = new FundViewHolder(View.inflate(mContext, R.layout.item_fund, null));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        AllProductDataBean productDataBean = allProductDataBeenList.get(position);

        BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
        baseViewHolder.bindData(productDataBean);

      /*  if (holder instanceof FinanceViewHolder) {

            FinanceViewHolder financeViewHolder = (FinanceViewHolder) holder;
            financeViewHolder.bindData(productDataBean);
        }


        FundViewHolder fundViewHolder = (FundViewHolder) holder;
        fundViewHolder.bindData(productDataBean);

        InvestViewHolder investViewHolder = (InvestViewHolder) holder;
        investViewHolder.bindData(productDataBean);
*/


    }

    public static abstract class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindData(AllProductDataBean productDataBean);
    }

    public static class FinanceViewHolder extends BaseViewHolder {

        @InjectView(R.id.item_finance_name)
        TextView itemFinanceName;
        @InjectView(R.id.item_finance_tag)
        TextView itemFinanceTag;
        @InjectView(R.id.item_finance_progress)
        ProgressRing itemFinanceProgress;
        @InjectView(R.id.item_finance_rate)
        TextView itemFinanceRate;
        @InjectView(R.id.item_finance_days)
        TextView itemFinanceDays;
        @InjectView(R.id.item_finance_action)
        Button itemFinanceAction;

        public FinanceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        //绑定数据
        public void bindData(AllProductDataBean productDataBean) {

            itemFinanceName.setText(productDataBean.getProductName());
            itemFinanceTag.setText(productDataBean.getPrdMark());
            try {
                itemFinanceProgress.setProgress(Float.parseFloat(productDataBean.getSellPer()));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            itemFinanceRate.setText(productDataBean.getPredictYearRate() + "");
            itemFinanceDays.setText(productDataBean.getTimeLimit() + "");
            itemFinanceAction.setEnabled(productDataBean.isPurchaseFlag());
        }
    }

    public static final String RISK_LEVLE_ITEMS[] = {"较低", "中等", "较高"};

    public static class FundViewHolder extends BaseViewHolder {


        @InjectView(R.id.item_fund_name)
        TextView itemFundName;
        @InjectView(R.id.textview_funddesc)
        TextView textviewFunddesc;
        @InjectView(R.id.textview_prdrisklevel)
        TextView textviewPrdrisklevel;
        @InjectView(R.id.textview_lowerapplyamount)
        TextView textviewLowerapplyamount;
        @InjectView(R.id.item_fund_net_worth)
        TextView itemFundNetWorth;
        @InjectView(R.id.item_fund_raise)
        TextView itemFundRaise;

        public FundViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        public void bindData(AllProductDataBean productDataBean) {

            itemFundName.setText(productDataBean.getProductName());
            textviewFunddesc.setText(productDataBean.getFundDesc());
            //prdRiskLevel: 2, 3, 4 较低，中等，较高
            textviewPrdrisklevel.setText(RISK_LEVLE_ITEMS[productDataBean.getPrdRiskLevel() - 2]);
            textviewLowerapplyamount.setText(productDataBean.getLowerApplyAmount() + "");
            itemFundNetWorth.setText(productDataBean.getNewUnit() + "");
            itemFundRaise.setText(productDataBean.getLastYearIncreaseStr());
        }
    }

    public static class InvestViewHolder extends BaseViewHolder {

        @InjectView(R.id.item_invest_name)
        TextView itemInvestName;
        @InjectView(R.id.item_invest_progress)
        ProgressRing itemInvestProgress;
        @InjectView(R.id.item_invest_rate)
        TextView itemInvestRate;
        @InjectView(R.id.item_invest_days)
        TextView itemInvestDays;
        @InjectView(R.id.item_invest_action)
        Button itemInvestAction;

        public InvestViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        public void bindData(AllProductDataBean productDataBean) {

            itemInvestName.setText(productDataBean.getProductName());
            try {
                itemInvestProgress.setProgress(Float.parseFloat(productDataBean.getSellPer()));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            itemInvestRate.setText(productDataBean.getPredictYearRate() + "");
            itemInvestDays.setText(productDataBean.getTimeLimit() + "");
            itemInvestAction.setEnabled(productDataBean.isPurchaseFlag());
        }
    }


    //分栏的返回类型
    @Override
    public int getItemViewType(int position) {
        return allProductDataBeenList.get(position).type;
    }

    @Override
    public int getItemCount() {
        return allProductDataBeenList == null ? 0 : allProductDataBeenList.size();
    }
}
